package com.lianyi.gaodeapi.util

import android.app.Activity
import android.app.AlertDialog
import android.content.Context
import android.content.Intent
import android.location.LocationManager
import android.provider.Settings
import com.lianyi.gaodeapi.dialog.GpsDialog

object GpsUtil {
    const val REQUEST_CODE_OPEN_GPS = 1008
    const val REQUEST_CODE_OPEN_GPS_BLUE = 1009
    const val REQUEST_CODE_OPEN_GPS_READ_CARD = 1010

    /**
     * 判断GPS是否开启，GPS或者AGPS开启一个就认为是开启的
     *
     * @param context
     * @return true 表示开启
     */
    fun isOPen(context: Context): Boolean {
        val locationManager = context.getSystemService(Context.LOCATION_SERVICE) as LocationManager
        // 通过GPS卫星定位，定位级别可以精确到街（通过24颗卫星定位，在室外和空旷的地方定位准确、速度快）
        val gps = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
        // 通过WLAN或移动网络(3G/2G)确定的位置（也称作AGPS，辅助GPS定位。主要用于在室内或遮盖物（建筑群或茂密的深林等）密集的地方定位）
        val network = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
        val isOp = gps || network
        return isOp
    }

    fun openGps(mActivity: Activity, questCode: Int = REQUEST_CODE_OPEN_GPS) {
        GpsDialog(mActivity, object : GpsDialog.onGpsClickListener {
            override fun confirm() {
                val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
                mActivity.startActivityForResult(intent, questCode)
            }

        }).show()
//        AlertDialog.Builder(mActivity)
//                .setTitle("提示")
//                .setMessage("当前手机扫描蓝牙需要打开定位功能")
//                .setNegativeButton("取消"
//                ) { dialog, which -> /*mActivity.finish() */ }
//                .setPositiveButton("前往设置"
//                ) { dialog, which ->
//                    val intent = Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS)
//                    mActivity.startActivityForResult(intent, questCode)
//                }
//
//                .setCancelable(false)
//                .show()
    }

}